<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/jquery-3.1.1.min.js"></script>
</head>

<body>
    <input type="file" id="updateInput" onchange="fileChange()">
    <a href="" id="download">下载文件</a>
    <script>
        function fileChange(e) {
            let evt = e || window.event;
            let file = evt.target.files[0];
            let reader = new FileReader();
            let reader2 = new FileReader();
            let rs = reader.readAsArrayBuffer(file);
            let blob = null;
            reader.onload = (e) => {
                if (typeof e.target.result === 'object') {
                    blob = new Blob([e.target.result])
                } else {
                    blob = e.target.result
                }
                console.log(blob);
                reader2.readAsText(blob, 'utf-8');
                reader2.onload = function (e) {
                    console.info(reader2.result);
                    var blob2 = new Blob([reader2.result], {
                        type: 'text/plain'
                    });
                    console.log(blob2);
                    const blobUrl = window.URL.createObjectURL(blob2);
                    const a = document.createElement('a');
                    a.href = blobUrl;
                    a.download = 'file.docx';
                    a.click();
                    window.URL.revokeObjectURL(blobUrl);
                }
            }
        }
        $('#download').on('click', function () { //尝试下载第一个同学的文件
            //var blob = JSON.parse(localStorage.getItem('blob'));
            //console.log(blob);
            //let reader = new FileReader();
            //console.log(reader.readAsDataURL(blob));
            //let binaryData = [];
            //binaryData.push(blob);
            //var blobUrl = window.URL.createObjectURL(new Blob(binaryData));
            //const a = document.createElement('a');
            //a.href = blobUrl;
            //a.download = 'file.docx';
            //a.click();
            //window.URL.revokeObjectURL(blobUrl);
            $.get('/file', {
                "download": 1
            }, function (re) {
                if (re != '0') {
                    var blob = re;
                    const blobUrl = window.URL.createObjectURL(blob);
                    const a = document.createElement('a');
                    a.href = blobUrl;
                    a.download = 'file.docx';
                    a.click();
                    window.URL.revokeObjectURL(blobUrl);
                } else
                    alert("下载失败");
            })
        })
    </script>

</body>

</html>